﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace PhotoDownloader
{
    class SitesMerger
    {
        private List<string> links;

        public SitesMerger()
        {
            links = new List<string>();
        }

        public SitesMerger(List<string> links)
        {
            this.links = links;
        }

        public List<string> FindImages()
        {
            List<string> images = new List<string>();
            //Console.WriteLine("Count of links: " + links.Count);
            foreach (string link in links)
            {
                List<string> nextPart = CodeAnalyzer.FindDefinedPhrases(CodeAnalyzer.DownloadCode(link), "jpg", "\"", "\"");
                //Console.WriteLine("Part count: " + nextPart.Count);
                images.AddRange(nextPart);
            }

            Console.WriteLine(images.Count);

            return images;
        }

        public void DownloadFiles(List<string> files, string pre = "")
        {
            WebClient client = new WebClient();
            List<string> result = new List<string>();
            List<string> fileNames = new List<string>();

            string path = "C:/outFiles/";

            for (int i = 0; i < files.Count; i++)
            {
                result.Add(pre + files.ElementAt(i));
                string newName = files.ElementAt(i).Replace("/", "");
                newName = newName.Replace("\\", "");
                fileNames.Add(newName);

                    //client.DownloadFile(result.ElementAt(i), path + fileNames.ElementAt(i));
                client.DownloadFile(result.ElementAt(i), path + "fileDown" + i + ".jpg");
  
                Console.WriteLine("Downloaded file: " + result.ElementAt(i));
            }




        }
    }
}
